package com.ws.shop.action.front;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import com.ws.shop.bean.FileDir;
import com.ws.shop.bean.Page;
import com.ws.shop.entity.Accessory;
import com.ws.shop.entity.Address;
import com.ws.shop.entity.Letter;
import com.ws.shop.entity.Member;
import com.ws.shop.entity.Message;
import com.ws.shop.entity.Order;
import com.ws.shop.entity.Payment;
import com.ws.shop.entity.Sunpic;
import com.ws.shop.service.AccessoryService;
import com.ws.shop.service.AuctionLogService;
import com.ws.shop.service.AuctionService;
import com.ws.shop.service.LetterService;
import com.ws.shop.service.LoginlogService;
import com.ws.shop.service.MemberService;
import com.ws.shop.service.MessageService;
import com.ws.shop.service.OrderService;
import com.ws.shop.service.PackageService;
import com.ws.shop.service.PaymentService;
import com.ws.shop.service.PointclogService;
import com.ws.shop.service.SunpicService;
import com.ws.shop.service.TicketService;
import com.ws.shop.service.TransactionService;
import com.ws.shop.util.DateUtil;
import com.ws.shop.util.ImageHelper;
import com.ws.shop.util.RequestUtil;
import com.ws.shop.util.StringUtil;

@Controller
@RequestMapping(value = "/member/*")
public class MemberAction   extends BaseFrontAction  {

	private static Logger log = Logger.getLogger(MemberAction.class);
	@Resource(name="fileDir")
	private FileDir fileDir;
	long maxSize = 500;//最大500K
	private static int fileSeq = 0;
	@Autowired
	private MemberService memberService;
	@Autowired
	private LoginlogService loginlogService;
	@Autowired
	private LetterService letterSerivce;
	@Autowired
	private OrderService orderService;
	@Autowired
	private MessageService messageService;
	@Autowired
	private TicketService ticketService;
	@Autowired
	private PointclogService pointclogService;
	@Autowired
	private  AuctionLogService auctionlogService;
	@Autowired
	private PaymentService paymentService;
	@Autowired
	private SunpicService sunpicService;
	@Autowired
	private PackageService packageService;
	@Autowired
	private TransactionService transactionService;
	@Autowired
	private AccessoryService accessoryService;
	@Autowired
	private AuctionService auctionService;
	//public $user = null;
	
	protected String initialize() {
		return "initialize";
	}
	
	public String index(){
		return "index";
	}
	
	public String mb_index(){
		return "index";
	}
	
	//保存优先使用点
	@RequestMapping(value = "savesetting.htm", method = RequestMethod.GET)
	public String saveSetting(HttpServletRequest request,HttpServletResponse response){
		int priority = RequestUtil.getIntParameter(request, "setting", 0);
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		if(uid == null){
			return this.ajaxJsonErrorMessage(response, "请登陆!");
		}
		if (priority!=0){
			int info = memberService.updateUserPriority(uid, priority);
			if (info>0){
				return this.ajaxJsonSuccessMessage(response, "操作成功!");
			}else{
				return this.ajaxJsonErrorMessage(response, "请登陆!");
			}
		}
		return this.ajaxJsonErrorMessage(response, "参数异常!");
	}
	
	
	//登陆日志
	@RequestMapping(value = "loginlog.htm", method = RequestMethod.GET)
	public String loginlog(HttpServletRequest request,Model model){
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		Page page = new Page(pageno, 10);
		loginlogService.getLoginInfoByUserId(page, uid);
    	model.addAttribute("page", page);
        return "loginlog";
	}
	
	
	//地址薄
	@RequestMapping(value = "disaddress.htm", method = RequestMethod.GET)
	public String disaddress(){
		return "disaddress";
	}
	
	@RequestMapping(value = "dis_address_add.htm", method = RequestMethod.GET)
	public String dis_address_add(){
		return "addressadd";
	}
	
	@RequestMapping(value = "address_insert.htm", method = RequestMethod.POST)
	public String address_insert(HttpServletRequest request,HttpServletResponse response){
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		if(uid == null){
			return this.ajaxJsonErrorMessage(response, "请登陆!");
		}
		String username = (String)loginMember.get(Member.LOGIN_MEMBER_NAME_KEY); 
		String reciver = RequestUtil.getStringParameter(request, "reciver", null);//收件人
		String postcode = RequestUtil.getStringParameter(request, "postcode", null); //邮编
		String addr = RequestUtil.getStringParameter(request, "address", null);
		String phoneno = RequestUtil.getStringParameter(request, "phoneno", null);
		//String memo = RequestUtil.getStringParameter(request, "memo", null);
		Address address = new Address();
		address.setUid(uid);
		address.setName(username);
		//address.setMemo(memo);
		address.setPhoneno(phoneno);
		address.setReciver(reciver);
		address.setAddress(addr);
		address.setPostcode(postcode);
		int count = memberService.getAddressCount(uid);
		if (count < 3 ){
			int info = memberService.saveAddress(address);
			if (info>0){
				return this.ajaxJsonSuccessMessage(response, "添加地址成功!");
			}else {
				return this.ajaxJsonErrorMessage(response, "添加失败!");
			}
		}else {
			return this.ajaxJsonErrorMessage(response, "您最多可以输入三个物流信息！");
		}
	}
	
	
	@RequestMapping(value = "address_edit.htm", method = RequestMethod.GET)
	public String  address_edit(HttpServletRequest request,Model model){
		long id = RequestUtil.getLongParameter(request, "id", 0);
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		if(uid!=null){
		   Address address = memberService.getAddressById(id);
		   model.addAttribute("address", address);
		}
		return "address_edit";
	}
	
	
	@RequestMapping(value = "ajax_address_update.htm", method = RequestMethod.POST)
	public String ajax_address_update(HttpServletRequest request,HttpServletResponse response){
		long id = RequestUtil.getLongParameter(request, "id", 0);
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		if(uid == null){
			return this.ajaxJsonErrorMessage(response, "请登陆!");
		}
		String reciver = RequestUtil.getStringParameter(request, "reciver", null);//收件人
		String postcode = RequestUtil.getStringParameter(request, "postcode", null); //邮编
		String addr = RequestUtil.getStringParameter(request, "address", null);
		String phoneno = RequestUtil.getStringParameter(request, "phoneno", null);
		//String memo = RequestUtil.getStringParameter(request, "memo", null);
		Address address = new Address();
		address.setId(id);
		address.setUid(uid);
		//address.setMemo(memo);
		address.setPhoneno(phoneno);
		address.setReciver(reciver);
		address.setAddress(addr);
		address.setPostcode(postcode);
		int info = memberService.updateAddressByid(address);
	    if(info>0){
	    	return this.ajaxJsonSuccessMessage(response, "修改成功!");
	    }else{
	    	return this.ajaxJsonErrorMessage(response, "修改失败!");
	    }
	}
	
	@RequestMapping(value = "address_del.htm", method = RequestMethod.GET)
	public String address_del(HttpServletRequest request,HttpServletResponse response){
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		if(uid == null){
			return this.ajaxJsonErrorMessage(response, "请登陆!");
		}
		long id = RequestUtil.getLongParameter(request, "id", 0);
		if(id!=0){
			int c =  memberService.delAddressById(id);
			if(c>0){
				return this.ajaxJsonSuccessMessage(response, "删除成功!");
			}else{
				return this.ajaxJsonErrorMessage(response, "操作失败!");
			}
		}else{
			return this.ajaxJsonErrorMessage(response, "参数异常!");
		}
		
	}
	
	@RequestMapping(value = "addresslist.htm", method = RequestMethod.GET)
	public String  addresslist (HttpServletRequest request,Model model){
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		if(loginMember != null){			
			long userid = (Integer) loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
			if(userid != 0){
				List<Address> addresslist = memberService.getAddressByUid(userid);
				model.addAttribute("useraddress", addresslist);
			}
		}
		return "addresslist";
	}
	
	@RequestMapping(value = "addressinsert.htm", method = RequestMethod.GET)
	public String  addressinsert (HttpServletRequest request,Model model){
		return "addressinsert";
	}
	
		
	//站内信息
	@RequestMapping(value = "website_message.htm", method = RequestMethod.GET)
	public String website_message(HttpServletRequest request,Model model){
		int type = RequestUtil.getIntParameter(request, "type", 0);
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		String username =(String)loginMember.get(Member.LOGIN_MEMBER_NAME_KEY); 
		//List<Letter> memberlist = letterSerivce.getLetters(type, username);
		long pageno = RequestUtil.getLongParameter(request, Page.PAGE_NO, 1);
		int pagesize = RequestUtil.getIntParameter(request, Page.PAGE_SIZE, 1);
		Page page = letterSerivce.getLetterByUserId(pageno, pagesize, username);
		model.addAttribute("page", page);
		//model.addAttribute("type", type);
		return "website_message";
	}
	 
   //删除站内信息
	@RequestMapping(value = "deleteselect.htm", method = RequestMethod.GET)
	public String deleteselect(HttpServletRequest request,HttpServletResponse response){
		long id = RequestUtil.getLongParameter(request, "id", 0);
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		String username = (String)loginMember.get(Member.LOGIN_MEMBER_NAME_KEY);
        int info = letterSerivce.delLetter(0, id, username);
		if (info>0){
			return this.ajaxJsonSuccessMessage(response, "删除成功!");
		}else{
			return this.ajaxJsonErrorMessage(response, "操作失败!");
		}
	
	}
	
	@RequestMapping(value = "send_message.htm", method = RequestMethod.GET)
	public String send_message(HttpServletRequest request,HttpServletResponse response){
		String receptionname = RequestUtil.getStringParameter(request, "receptionname", null);//收信人
		String message = RequestUtil.getStringParameter(request, "message", null); 
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		String username =(String)loginMember.get(Member.LOGIN_MEMBER_NAME_KEY);
		Letter letter = new Letter();
		letter.setMessage(message);
		letter.setReceptionname(receptionname);
		letter.setSendname(username);
		letter.setType(1);
		int info = letterSerivce.sendMessage(letter);
		if (info>0){
			return this.ajaxJsonSuccessMessage(response, "发送成功!");
		}else {
			return this.ajaxJsonErrorMessage(response, "发送失败!");
		}
	}
	
	
	//修改个人信息
	@RequestMapping(value = "modify.htm", method = RequestMethod.GET)
	public String modify(){
		return "modify";
	}
	
	//修改头像
	@RequestMapping(value = "modify_logo.htm", method = RequestMethod.GET)
	public String  modify_logo(HttpServletRequest request,HttpServletResponse response){
		
		return this.ajaxJsonSuccessMessage(response, "修改成功!");
	}
	
	//修改基本信息
	@RequestMapping(value = "modify_logo.htm", method = RequestMethod.GET)
	public String modify_info(HttpServletRequest request,HttpServletResponse response){
		String realname = RequestUtil.getStringParameter(request, "realname", null);
		int sex = RequestUtil.getIntParameter(request, "sex", 0); 
		String birthday = RequestUtil.getStringParameter(request, "birthday", null);
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		Member member = new Member();
		member.setId(uid);
		member.setSex(sex);
		member.setRealname(realname);
		int info = -1;
		try {
			member.setBirthday(DateUtil.getDateFormat(DateUtil.YYMMDDHHMMSSSSS).parse(birthday));
			info = memberService.updateUsers(member);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		if (info>0){
			return this.ajaxJsonSuccessMessage(response, "修改成功!");
		}else {
			return this.ajaxJsonErrorMessage(response, "修改失败!");
		}
	}
	
	//修改密码页面
	@RequestMapping(value = "modifypwd.htm", method = RequestMethod.GET)
	public String modifypwd(HttpServletRequest request,HttpServletResponse response){
		return "modifypwd";
	}
	
	//修改密码处理方法
	public String modifypwdhan(HttpServletRequest request,HttpServletResponse response){
		String oldPassword = RequestUtil.getStringParameter(request, "oldPassword", null);
		String userPassword = RequestUtil.getStringParameter(request, "userPassword", null);
		if(StringUtil.isEmpty(userPassword)){
			return this.ajaxJsonErrorMessage(response, " 旧密码不为空 !");
		}else if(StringUtil.isEmpty(userPassword)) {
			return this.ajaxJsonErrorMessage(response, " 新密码不为空 !");
		}
		String md5old = DigestUtils.md5Hex(oldPassword);
		String m5new = DigestUtils.md5Hex(userPassword);
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		int c = memberService.updateUserPassword(uid, md5old, m5new);
        if(c==0){
        	return this.ajaxJsonSuccessMessage(response, "修改成功!");
        }else if(c==1){
        	return this.ajaxJsonErrorMessage(response, "旧密码错误!");
        }else if(c==2){
        	return this.ajaxJsonErrorMessage(response, "修改密码失败!");
        }else{
        	return this.ajaxJsonErrorMessage(response, "系统忙!");
        }
		
	}
	
	//安全认证
	public String safecount(){
		return "safecount";
	}
	
	
	//发送短信用户认证
	public String sendCode(){

		return "";
	}
	
	//_______________________________________________________________________________________
	//短信认证
	public String dosafecount() {
		
		return "";
	}
	
	//点券充值
	@RequestMapping(value = "actcard.htm", method = RequestMethod.GET)
	public String actcard(HttpServletRequest request,HttpServletResponse response){
		return "actcard";
	}
	
	//点券列表
	@RequestMapping(value = "cardList.htm", method = RequestMethod.GET)
	public String cardList(HttpServletRequest request,HttpServletResponse response){
		return "cardList";
	}
	
	//点券充值明细
	@RequestMapping(value = "chargeInfo.htm", method = RequestMethod.GET)
	public String chargeInfo(HttpServletRequest request,HttpServletResponse response){
		return "chargeInfo";
	}
	
	//点券验证充值页面
	@RequestMapping(value = "validecard.htm", method = RequestMethod.GET)
	public String validecard(HttpServletRequest request,HttpServletResponse response){
		return "validecard";
	}
	
	//点券验证充值
	@RequestMapping(value = "chargeCard.htm", method = RequestMethod.GET)
	public String chargeCard(HttpServletRequest request,HttpServletResponse response){
		String cardsn = RequestUtil.getStringParameter(request, "cardsn", null);
		String cardpwd = RequestUtil.getStringParameter(request, "cardpwd", null);
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		String username =(String)loginMember.get(Member.LOGIN_MEMBER_NAME_KEY);
		int c = ticketService.chargeCard(cardsn, cardpwd, uid, username);
		 if(c==0){
	        	return this.ajaxJsonSuccessMessage(response, "点券充值成功!");
	        }else if(c==1){
	        	return this.ajaxJsonErrorMessage(response, "验证错误，请正确填写点券信息!");
	        }else if(c==2){
	        	return this.ajaxJsonErrorMessage(response, "已经被使用!");
	        }else if(c==2){
	        	return this.ajaxJsonErrorMessage(response, "点券已经过期!");
	        }else{
	        	return this.ajaxJsonErrorMessage(response, "系统忙!");
	        }
	}

	//充值
	public String count(){
		
		return "count";
	}
	@RequestMapping(value = "countsingle.htm", method = RequestMethod.GET)
	public String countsingle(HttpServletRequest request,Model model){
		Payment alipay = paymentService.getPaymentByid(1);
		Payment yeepay = paymentService.getPaymentByid(2);
		Payment chinabank = paymentService.getPaymentByid(3);
		Payment tenpay = paymentService.getPaymentByid(4);
		model.addAttribute("alipay", alipay);
		model.addAttribute("yeepay", yeepay);
		model.addAttribute("chinabank", chinabank);
		model.addAttribute("tenpay", tenpay);
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		Page page = new Page(pageno, 10);
		packageService.queryPackages(page);
        model.addAttribute("page", page);
		return "countsingle";
	}
	
	//确认支付
	@RequestMapping(value = "affirmprepay.htm", method = RequestMethod.GET)
	public String affirmprepay(HttpServletRequest request,HttpServletResponse response){
		int paytype = RequestUtil.getIntParameter(request, "paytype", 0); 
		double paypoint_price = RequestUtil.getDoubleParameter(request, "paypoint_price", 0);
		long oid = RequestUtil.getLongParameter(request, "oid", 0);
		int pointtype = RequestUtil.getIntParameter(request, "pointtype", 0); 
		if(paypoint_price<=0){
			 return this.ajaxJsonErrorMessage(response, "金额不能为负数或0！");
		}
		String payment = "支付宝";	 
		if (paytype == 1){
			payment = "支付宝";	    	
		}else if(paytype == 2){
			payment = "易宝支付";
		}else if(paytype == 3){
			payment = "网银在线";
		}else if(paytype == 4){
			payment = "财付通";
		}
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		String username =(String)loginMember.get(Member.LOGIN_MEMBER_NAME_KEY); 
		
/*		if (oid!=0){
			$pid = GetNum($data['pointtype']);
			$package = D('Package');
		    $packagedata = $package->find($pid);
			if (empty($packagedata)){
				$paymentorderdata =array(
					'uid'=>$this->user['id'],
					'name'=>$this->user['name'],
					'payment'=>$payment,
					'amount'=>GetNum($data['paypoint_price'])
				);
			}else {
				$paymentorderdata =array(
					'uid'=>$this->user['id'],
					'name'=>$this->user['name'],
					'payment'=>$payment,
					'amount'=>$packagedata['amount'],
					'pid'=>$packagedata['id'],
				);
			}
			
			$paymentorder = D('Paymentorder');
			$val = $paymentorder->insert($paymentorderdata);
			$type = 'RE';
		}else {
			$order = D('Order');
			$val = $order->find($id);
			$type = 'PA';
		}
		if ($data['paytype'] == 1){
			import("@.ORG.Alipay");
	    	$alipay = new Alipay();
	    	$json = $alipay->_alipayto($val,$type);
		}elseif($data['paytype'] == 2){
			import("@.ORG.Yeepay");
	    	$alipay = new Yeepay();
	    	$json = $alipay->_alipayto($val,$type);
		}elseif($data['paytype'] == 3){
			import("@.ORG.Chinabank");
	    	$alipay = new Chinabank();
	    	$json = $alipay->_alipayto($val,$type);
		}elseif($data['paytype'] == 4){
			import("@.ORG.Tenpay");
	    	$alipay = new Tenpay();
	    	$json = $alipay->_alipayto($val,$type);
		}
		echo $json;*/
		return  "";
		
	}
	
	//竞拍统计
	 @RequestMapping(value = "countinfo_out.htm", method = RequestMethod.GET)
	public String  countinfo_out(HttpServletRequest request,Model model){
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		Page page = new Page(pageno, 10);
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		auctionlogService.getAuctionLogByUid(page, uid);
		model.addAttribute("page", page);
		return "countinfo_out";
	}
	
	//查看详细竞拍日志
	 @RequestMapping(value = "biddetail.htm", method = RequestMethod.GET)
	public String biddetail(HttpServletRequest request,Model model){
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		Page page = new Page(pageno, 10);
		long aid = RequestUtil.getLongParameter(request, "aid", 0);
		auctionlogService.getAuctionLogByUidAndAid(page, uid, aid);
	    model.addAttribute("page", page);
	    return "biddetail";
		
	}
	
	///支付统计
	 @RequestMapping(value = "chargelist.htm", method = RequestMethod.GET)
	public String  chargelist(HttpServletRequest request,Model model){
	   int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
	    Page page = new Page(pageno, 10);
	    Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
 
		    transactionService.displayUserTransaction(page, uid);
		    model.addAttribute("page", page);
		    return "chargelist";
	}
	
	//我的晒图
	 @RequestMapping(value = "feedback.htm", method = RequestMethod.GET)
	public String feedback(HttpServletRequest request,Model model){
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
	    Page page = new Page(pageno, 10);
	    Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		sunpicService.getFreeBack(page, uid);
	    model.addAttribute("page", page);
		return "feedback";
	}
	 
	//站外晒图
	 @RequestMapping(value = "winnerlink.htm", method = RequestMethod.GET)
	public String winnerlink(HttpServletRequest request,Model model){
		long id = RequestUtil.getLongParameter(request, "id", -1);
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		int state = 3;
		Order orderdata  = orderService.getOne(uid, id, state,1);
		if (orderdata!=null){
			model.addAttribute("id", orderdata.getId());
		}
		model.addAttribute("orderdata", orderdata);
		return "winnerlink";
	}
	
	//保存站外晒图
	 @RequestMapping(value = "winerlink_save.htm", method = RequestMethod.GET)
	public String winerlink_save(HttpServletRequest request,HttpServletResponse response){
		 long id = RequestUtil.getLongParameter(request, "id", -1);
	     String[] outlinks = request.getParameterValues("outlink");
		 String word = RequestUtil.getStringParameter(request, "word", null);
		 if(outlinks == null){
			 return this.ajaxJsonErrorMessage(response, "连接地址不能为空！");
		 }
		 Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		 Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		 int state = 3; 
	     int c = sunpicService.saveSunpic(uid, id, state, word, outlinks);
	     if(c==0){
	    	 return this.ajaxJsonSuccessMessage(response, "操作成功!");
	     }else{
	    	 return this.ajaxJsonErrorMessage(response, "存储失败！");
	     }
 
	}
	
	//站内晒图
	 @RequestMapping(value = "winersay.htm", method = RequestMethod.GET)
	public String winersay(HttpServletRequest request,Model model){
		long id = RequestUtil.getLongParameter(request, "id", -1);
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		int state = 3;
		Order orderdata  = orderService.getOne(uid, id, state,1);
		if (orderdata!=null){
			model.addAttribute("id", orderdata.getId());
		}
		model.addAttribute("orderdata", orderdata);
		return "winersay";
	}
	 
	//显示已上传的图片
	 @RequestMapping(value = "winersay_loadimg.htm", method = RequestMethod.GET)
	public String  winersay_loadimg(HttpServletRequest request,HttpServletResponse response) throws IOException{
		 long id = RequestUtil.getLongParameter(request, "id", -1);
		 Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		 Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		 int state = 3;
		 Order orderdata  = orderService.getOne(uid, id, state,1);

		if (orderdata!=null){
			List<Sunpic> sunlist = sunpicService.getSunpicByUidAndOid(uid, orderdata.getId());
		 
			if (sunlist!=null){
				Sunpic sunpic = sunlist.get(0);
				List<Accessory> acclist =  accessoryService.getAccessoryByRelationId(sunpic.getId());
				String s = "";
				StringBuffer html = new StringBuffer();
				for(int i=0;i<acclist.size();i++){
					Accessory  acc = acclist.get(i);
					html.append("<li><span><img src='/Public/accessory/img/"+acc.getThumbnail()+"'/></span><a href='javascript: onclick=winnersay_delimg('"+acc.getId()+"','"+orderdata.getId()+"'); id= '"+acc.getId()+"'>删除</a></li>" );
				}
				
				return this.ajaxHtml(response, html.toString());
			}
		} 
		return this.ajaxHtml(response, "");
	}
	
	//图片上传
	 @RequestMapping(value = "winersay_upimg.htm", method = RequestMethod.GET)
	public String  winersay_upimg(HttpServletRequest request,HttpServletResponse response){
		 long id = RequestUtil.getLongParameter(request, "id", -1);
		 Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);

		 long accessoryid = this.imgUpload();
			 int c = accessoryService.winersayUpimg(uid, id, accessoryid);
			 if(c==0){
					return this.ajaxJsonSuccessMessage(response, "操作成功");
				}else{
					return this.ajaxJsonErrorMessage(response, "操作错误!");
				}
	}
	
	 public long imgUpload(){
		 return 0;
	 }
	 
	//删除图片
	 @RequestMapping(value = "winersaydelimg.htm", method = RequestMethod.GET)
	public String winersaydelimg(HttpServletRequest request,HttpServletResponse response){
		 Map<String,Object> loginMember = getLoginMember(request.getSession(false));
			Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
			long id = RequestUtil.getLongParameter(request, "accid", -1);
			boolean is = memberService.isUserSunpic(id, uid);
			if(is){
				memberService.delImg(id);
			}
		 return "winersaydelimg";
	}
	
	
	//保存站内晒图
	@RequestMapping(value = "winersay_save.htm", method = RequestMethod.GET)
	public String  winersay_save(HttpServletRequest request,HttpServletResponse response){
		int id = RequestUtil.getIntParameter(request, "oid", -1);
		String word = RequestUtil.getStringParameter(request, "word", null);
		Order order = orderService.findOrderById(id);
		int c = memberService.winersaysave(order, word);
		if(c==0){
			return this.ajaxJsonSuccessMessage(response, "操作成功");
		}else{
			return this.ajaxJsonErrorMessage(response, "操作错误!");
		}
		 
	}
	
	
	//竞拍成功
	 @RequestMapping(value = "wingame.htm", method = RequestMethod.GET)
	public String wingame(HttpServletRequest request,Model model){
		int type = RequestUtil.getIntParameter(request, "type", -1);
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
	    Page page = new Page(pageno, 10);
	    Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		int ttype = 1 ; //竞拍的type=1
        orderService.orderPayment(page, ttype, uid);
        model.addAttribute("page", page);
        model.addAttribute("type", type);
		return "wingame";
	}
	
	
	//确认发货
	 @RequestMapping(value = "clinch.htm", method = RequestMethod.GET)
	public String clinch(HttpServletRequest request,HttpServletResponse response){
		 Map<String,Object> loginMember = getLoginMember(request.getSession(false));
			Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		long id = RequestUtil.getLongParameter(request, "id", -1);
		if (id == -1) {
			return this.ajaxJsonErrorMessage(response, "参数错误！");
		}else {
			int c = orderService.clinch(uid, id);
			if(c==0){
				  return this.ajaxJsonSuccessMessage(response, "确认发货,操作成功！");
			}else if (c==1){
				return this.ajaxJsonErrorMessage(response, "订单不存在！");
			}else if(c==2){
				return this.ajaxJsonErrorMessage(response, "非法操作！");
			}else if(c==3){
					return this.ajaxJsonErrorMessage(response, "订单状态不正确！");
			}else if(c==4){
				return this.ajaxJsonErrorMessage(response, "操作失败！");
			}else{
				return this.ajaxJsonErrorMessage(response, "系统忙！");
			}
		}
		
	}
	
	//保价购买
	 @RequestMapping(value = "addgame.htm", method = RequestMethod.GET)
	public String addgame(HttpServletRequest request,Model model){
		int type = RequestUtil.getIntParameter(request, "type", -1);
		//保价购买type 为0
		//统计条件
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
	    Page page = new Page(pageno, 10);
	    Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		int ttype = 0 ; //竞拍的type=1
        orderService.orderPayment(page, ttype, uid);
        model.addAttribute("page", page);
		if (type == 0){
			//$config = C('SYSCONFIG');
			List<Order> orderlist = page.getData();
			double rmb_beat = 1;
		 	for (int i= 0 ;i<orderlist.size();i++){
		 		Order order = orderlist.get(i);
		 		long aid = order.getAid();
				Map map = auctionlogService.mycount(aid, uid);
				int beatnum = (Integer)map.get("sumbeat");//闪点总和
				int bout = (Integer) map.get("sumpointb"); //次数
				double premium = beatnum/rmb_beat;  //换算差价金额
				order.setBeatnum(beatnum);
				order.setBout(bout);
				order.setPremium(premium);
			}
		}
		 model.addAttribute("page", page);
		 model.addAttribute("type", type);
	    return "addgame";
	}
	
	//失效订单
	 @RequestMapping(value = "losegame.htm", method = RequestMethod.GET)
	public String losegame(HttpServletRequest request,Model model){
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		Page page = new Page(pageno, 10);
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
	    int state = 5;
	    orderService.loseOrder(page, state, uid);
	    model.addAttribute("page", page);
	    return "losegame";
	}
	
	//显示支付订单
	 @RequestMapping(value = "showOrder.htm", method = RequestMethod.GET)
	public String showOrder(HttpServletRequest request,Model model){
		 /**
		long id = RequestUtil.getLongParameter(request, "id", -1);
		String uid = (String)getSession(request).getAttribute(Member.LOGIN_MEMBER_UID_SESSION_NAME);
	    long userid = Long.valueOf(uid);
	    int state = 0;
	    Order order  = orderService.getOne(userid, id, state,1);
		if (order!=null){
			//补差价信息
			double rmb_beat = 1;
			long aid = order.getAid();
			Map map = auctionlogService.mycount(aid, userid);
			int beatnum = (Integer)map.get("sumbeat");//闪点总和
			int bout = (Integer) map.get("sumpointb"); //次数
			double premium = beatnum/rmb_beat;  //换算差价金额
			order.setBeatnum(beatnum);
			order.setBout(bout);
			order.setPremium(premium);
		
		}
		model.addAttribute("order", order);
		**/
		return "showOrder";
	}
		
	//使用地址
	 @RequestMapping(value = "userAddress.htm", method = RequestMethod.GET)
	public String userAddress(HttpServletRequest request,Model model){
		 Map<String,Object> loginMember = getLoginMember(request.getSession(false));
			Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		List<Address> addresslist = memberService.getAddressByUid(uid);
		int n = 1;
		model.addAttribute("data", addresslist);
		model.addAttribute("n", n);
		return "userAddress";
	}
	
	//保存订单
	 @RequestMapping(value = "saveOrder.htm", method = RequestMethod.GET)
	public String saveOrder(HttpServletRequest request,Model model){
		 Map<String,Object> loginMember = getLoginMember(request.getSession(false));
			Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		String userRealName = RequestUtil.getStringParameter(request, "userRealName", null);
		String userPhoneNumber = RequestUtil.getStringParameter(request, "userPhoneNumber", null);
		String zipCode = RequestUtil.getStringParameter(request, "zipCode", null);
		String detailedAddress = RequestUtil.getStringParameter(request, "detailedAddress", null);
		String remarks = RequestUtil.getStringParameter(request, "remarks", null);
		long id = RequestUtil.getLongParameter(request, "id", -1);
		Order order = new Order();
		order.setId(id);
		order.setRecipient(userRealName);
		order.setTel(userPhoneNumber);
		order.setPostalcode(zipCode);
		order.setAddress(detailedAddress);
		order.setRemark(remarks);
		order.setUid(uid);
		int state = 0;
		order.setState(state);
		int c = orderService.updateOrder(order);
		if(c!= 0 ){
			return "errsaveorder";
		}
		
		Order orderdata  = orderService.getOne(uid, id, state,2);
		if (orderdata!=null){
				//补差价信息
				double rmb_beat = 1;
				long aid = order.getAid();
				Map map = auctionlogService.mycount(aid, uid);
				int beatnum = (Integer)map.get("sumbeat");//闪点总和
				int bout = (Integer) map.get("sumpointb"); //次数
				double premium = beatnum/rmb_beat;  //换算差价金额
				order.setBeatnum(beatnum);
				order.setBout(bout);
				order.setPremium(premium);
			
		}
		model.addAttribute("order", orderdata);
		Payment alipay = paymentService.getPaymentByid(1);
		Payment yeepay = paymentService.getPaymentByid(2);
		Payment chinabank = paymentService.getPaymentByid(3);
		Payment tenpay = paymentService.getPaymentByid(4);
		model.addAttribute("alipay", alipay);
		model.addAttribute("yeepay", yeepay);
		model.addAttribute("chinabank", chinabank);
		model.addAttribute("tenpay", tenpay);	
		return "saveOrder";
	}
	
	
	

	//打卡
	 @RequestMapping(value = "doExpoint.htm", method = RequestMethod.GET)
	public String doExpoint(HttpServletRequest request,HttpServletResponse response){
		 Map<String,Object> loginMember = getLoginMember(request.getSession(false));
			Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
        int c = memberService.doExpoint(uid);
	    if(c==0){
	    	return this.ajaxJsonSuccessMessage(response, "打卡成功!");//成功
	    }else if(c==1){
	    	return this.ajaxJsonErrorMessage(response, "用户不存在!!");
	    }else if(c==2){
	    	return this.ajaxJsonErrorMessage(response, "无效用户!!");
	    }else if(c==3){
	    	return this.ajaxJsonErrorMessage(response, "已经打过卡!!");
	    }else if(c==4){
	    	return this.ajaxJsonErrorMessage(response, "打卡失败!!");
	    }else{
	    	return this.ajaxJsonErrorMessage(response, "系统忙!!");
	    }

	}
    
	//保存留言
	 @RequestMapping(value = "saveMessage.htm", method = RequestMethod.GET)
	public String saveMessage(HttpServletRequest request,HttpServletResponse response){
		String msg = RequestUtil.getStringParameter(request, "message", null);
		String title = RequestUtil.getStringParameter(request, "title", null);
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		String username =(String)loginMember.get(Member.LOGIN_MEMBER_NAME_KEY); 
		
		if(StringUtil.isEmpty(title)){
			return this.ajaxJsonErrorMessage(response, "标题不能为空!!");
		}
		if(StringUtil.isEmpty(msg)){
			return this.ajaxJsonErrorMessage(response, "想拍什么？写下来");
		}
		Message message = new Message();
		message.setUid(uid);
		message.setName(username);
		message.setIp(request.getRemoteAddr());
		message.setTitle(title);
		message.setMessage(msg);
		int info =messageService.saveMessage(message);
		 if (info>0){
			return this.ajaxJsonSuccessMessage(response, "保存成功!");//成功
		}else {
			return this.ajaxJsonErrorMessage(response, "保存失败");//失败
		}

	}

	//积分日志
	 @RequestMapping(value = "scorelist.htm", method = RequestMethod.GET)
	public String dspointclog(HttpServletRequest request,Model model){
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
	    Page page = new Page(pageno, 10);
	    Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
	    pointclogService.displayUserPointc(page, uid);
	    model.addAttribute("page", page);
		return "scorelist";
	}
	
	//兑换订单
	 @RequestMapping(value = "scoreBuy.htm", method = RequestMethod.GET)
	public String mallorder(HttpServletRequest request,Model model){
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		//统计条件
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
	    Page page = new Page(pageno, 10);
		//数据条件
	    int type =2;
        orderService.mallorder(page, type, uid);
        model.addAttribute("page", page);
		return "scoreBuy";
	}
	 public synchronized static String getFileSeq() {
			if (fileSeq < 999) {
				fileSeq++;
			} else {
				fileSeq = 1;
			}
			// 数字格式化
			NumberFormat nf = NumberFormat.getInstance();
			nf.setMinimumIntegerDigits(3);
			nf.setGroupingUsed(false);
			return DateUtil.dateToString(new Date(), DateUtil.YYYYMMDDHHMMSS)
					+ nf.format(fileSeq);
		}
	//上传头像
	@RequestMapping(value = "uploadHead.htm", method = RequestMethod.GET)
	public String uploadHead(HttpServletRequest request,HttpServletResponse response){
		try{
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		String username =(String)loginMember.get(Member.LOGIN_MEMBER_NAME_KEY);
		FileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setHeaderEncoding("UTF-8");
		List<FileItem> items = (List<FileItem>)upload.parseRequest(request);
		Iterator<FileItem> itr = items.iterator();
		FileItem item = null;
		while (itr.hasNext()) {
			item = itr.next();
			String fileName = item.getName();
			if (!item.isFormField()) {
				//检查文件大小
				if(item.getSize() > maxSize){
					return this.ajaxJsonErrorMessage(response, "上传文件大小超过最大限制。");
				}
				InputStream inputstream = item.getInputStream();
				boolean isImage = ImageHelper.validateImageType(inputstream);
				if(!isImage){
					inputstream.close();
					return this.ajaxJsonErrorMessage(response, "不是图片格式");
				}
				//检查扩展名
				String fileExt = "";
				if(fileName.indexOf(".") >= 0)
					fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
				String newFileName = username + getFileSeq() + "." + fileExt;
				try{
					File uploadedFile = new File(fileDir.head, newFileName);
					item.write(uploadedFile);
				}catch(Exception e){
					inputstream.close();
					return this.ajaxJsonErrorMessage(response, "上传文件失败");
				}
				String path = request.getContextPath();
				String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
				basePath = basePath + "/headimg/b/";
				try{
					int c = memberService.updateHead(newFileName, uid);
					if(c>0){
						Map<String,String> jsonMap = new HashMap<String,String>();
						jsonMap.put(STATUS, "success");
						jsonMap.put(MESSAGE, "上传成功");
						jsonMap.put("saveURL", basePath + newFileName);
						jsonMap.put("id", String.valueOf(uid));
						return this.ajaxJson(response, jsonMap);
					}else{
						File f = new File(fileDir.head, newFileName);
						if(f.exists()){
							f.delete();
						}
					}
				}catch(Exception e){
					log.error("保存文件信息失败",e);
					File f = new File(fileDir.head, newFileName);
					if(f.exists()){
						f.delete();
					}
					return this.ajaxJsonErrorMessage(response, "系统异常，请稍后再试");
				}finally{
					if(inputstream != null){
						inputstream.close();
					}
				}
			}
		}
		return this.ajaxJsonErrorMessage(response, "没有选中要上传的文件");
		}catch(Exception ex){
			ex.printStackTrace();
			log.info(ex.getMessage(),ex);
			return this.ajaxJsonErrorMessage(response, "系统忙！");
		}
	 
	}

	 // 请求地址
	 //确认返点
	 @RequestMapping(value = "rebeat.htm", method = RequestMethod.GET)
	 public String rebeat(HttpServletRequest request,HttpServletResponse response){
	  long id = RequestUtil.getLongParameter(request, "id", -1);//订单的ID
	  Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
	  if (id == -1) {
	   return this.ajaxJsonErrorMessage(response, "参数错误!");
	  }else {
		  int c = memberService.rebeat(id, uid);
		  if(c==0){
			  return this.ajaxJsonSuccessMessage(response, "已返点,操作成功！");
		  }else if(c==1){
			  return this.ajaxJsonErrorMessage(response, "订单不存在!"); 
		  }else if(c==2){
			  return this.ajaxJsonErrorMessage(response, "非法操作!"); 
		  }else if(c==3){
			  return this.ajaxJsonErrorMessage(response, "订单状态不正确!"); 
		  }else{
			  return this.ajaxJsonErrorMessage(response, "系统忙!"); 
		  }
	   
	  }
	  
	 }

	 
	/**
	 * 正在参与的竞拍页面
	 * @param request
	 * @param response
	 * @return
	 */
	 @RequestMapping(value = "bidding.htm", method = RequestMethod.GET)
	 public String bidding(HttpServletRequest request,Model model){
		 int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		 Page page = new Page(pageno, 10);
		 Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		 Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		 auctionService.userAuctioningList(page, uid);
		 model.addAttribute("page", page);
		 return "bidding";
	 }
	 
	 
	 
   /**
	* 已经参与过的竞拍页面
	* @param request
	* @param response
	* @return
	*/
	@RequestMapping(value = "bided.htm", method = RequestMethod.GET)
	public String bided(HttpServletRequest request,Model model){
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		Page page = new Page(pageno, 10);
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		auctionlogService.getAuctionLogByUid(page, uid);
		model.addAttribute("page", page);
		return "bided";
	}
	

   /**
	* 点券出价记录
	* @param request
	* @param response
	* @return
	*/
	@RequestMapping(value = "bidHistory.htm", method = RequestMethod.GET)
	public String bidHistory(HttpServletRequest request,HttpServletResponse response){
		return "bidHistory";
	}
	
	
   /**
	* 
	* 邀请好友页面
	* @param request
	* @param response
	* @return
	* 
	*/
	@RequestMapping(value = "recommend.htm", method = RequestMethod.GET)
	public String recommend(HttpServletRequest request,Model model){
		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		Integer uid = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
		List<Member> listmember = memberService.getChirlds(uid);
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		basePath = basePath + "regist.htm?code="+uid;
		model.addAttribute("friendurl", basePath);
		model.addAttribute("data", listmember);
		return "recommend";
	}
	
	
}
